﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TlToolCordCreate
{
    /// <summary>
    /// uc.xaml 的交互逻辑
    /// </summary>
    public partial class ucSwaggerToCode : UserControl
    {
        public ucSwaggerToCode()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            List<string> input = new List<string>();
            List<string> outstr = new List<string>();
            int jieduan = 0;
            string codestr = code.Text;
            var data1 = codestr.Split("\r\n");
            for (int i = 0; i < data1.Length; i++)
            {
                data1[i] = data1[i].Trim();
                if (data1[i]== "参数名 数据类型 参数类型 是否必填 说明")
                {
                    jieduan = 1;
                }
                if (data1[i] == "状态码 描述 说明")
                {
                    jieduan = 3;
                }
                if (data1[i] == "返回属性名 类型 说明")
                {
                    jieduan = 4;
                }
                if (data1[i] == "请求示例")
                {
                    jieduan = 6;
                }
                if (jieduan==1)
                {
                    jieduan++;
                }
                else if (jieduan==2)
                {
                    input.Add(data1[i]);
                }
                else if (jieduan==4)
                {
                    jieduan++;
                }
                else if (jieduan == 5)
                {
                    outstr.Add(data1[i]);
                }
            }
            for (int i = 0; i < input.Count; i++)
            {
                string code1 = "/// <summary>\r\n";  
                string code2 = "///"; 
                string code3 = " /// </summary>\r\n"; 
                string code = "public ";
                var data2= input[i].Split(" ");
                for (Int32 j = 0; j < data2.Length; j++)
                {
                    if (data2[j].Contains("String")||
                        data2[j].Contains("string"))
                    {
                        code += "string ";
                        continue;
                    }
                    if (data2[j].Contains("array")||
                    data2[j].Contains("list"))
                    {
                        code += "List<object> ";
                        continue;
                    }
                    if (data2[j].Contains("int"))
                    {
                        code += "int ";
                        continue;
                    }
                    if (data2[j].Contains("object"))
                    {
                        code += "object ";
                        continue;
                    }
                    //List
                    if (data2[j].Contains("List"))
                    {
                        code += "List<object> ";
                        continue;
                    }
                }
                var data3= data2[0].Split(".");
                if (data3.Length>0)
                {
                    code += data3[data3.Length - 1];
                }
                else
                {
                    code += data2[0];
                }
                code += " {get;set;}";
                code2 += data2[data2.Length - 1] + "\r\n";
                input[i] = code1+ code2+ code3+ code;
            }

            for (int i = 0; i < outstr.Count; i++)
            {
                string code1 = "/// <summary>\r\n";
                string code2 = "///";
                string code3 = " /// </summary>\r\n";
                string code = "public ";
                var data2 = outstr[i].Split(" ");
                for (Int32 j = 0; j < data2.Length; j++)
                {
                    if (data2[j].Contains("String") ||
                        data2[j].Contains("string"))
                    {
                        code += "string ";
                        continue;
                    }
                    if (data2[j].Contains("array") ||
                  data2[j].Contains("list"))
                    {
                        code += "List<object> ";
                        continue;
                    }
                    if (data2[j].Contains("int"))
                    {
                        code += "int ";
                        continue;
                    }
                    if (data2[j].Contains("object"))
                    {
                        code += "object ";
                        continue;
                    }
                    if (data2[j].Contains("List"))
                    {
                        code += "List<object> ";
                        continue;
                    }
                }
                var data3 = data2[0].Split(".");
                if (data3.Length > 0)
                {
                    code += data3[data3.Length - 1];
                }
                else
                {
                    code += data2[0];
                }
                code += " {get;set;}";
                code2 += data2[data2.Length - 1] + "\r\n";
                outstr[i] = code1 + code2 + code3 + code;
            }

            string codes = "";
            foreach (var item in input)
            {
                codes += item+"\r\n";
            }
            dstcode.Text = codes;
            codes = "";
            foreach (var item in outstr)
            {
                codes += item + "\r\n";
            }
            dstcode2.Text = codes;
        }
    }
}
